/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package DAO;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;

/**
 *
 * @author tran tuan tai
 */
public class DAOAccess
{

    private static String m_server;
    private static String m_user;
    private static String m_pass;
    private static int    m_port;
    private static String m_databaseName;
    private static Connection m_conn;

    private static DAOAccess m_instance = new DAOAccess("localhost", "contentfilter", "root", "123", 3306);

     public static ResultSet ExcuteQuery(String query)
    {
        try
        {
            java.sql.Statement statement = m_conn.createStatement();
            if (statement.execute(query))
            {
                return statement.getResultSet();
            }
            else
            {
                System.out.println("Cannot excute statement: " + query);
            }

        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }

        return null;
    }

    public static ResultSet ExecuteQuery(String query)
    {
        try
        {
            java.sql.Statement statement = m_conn.createStatement();
            if (statement.execute(query))
            {
                return statement.getResultSet();
            }
            else
            {
                System.out.println("Cannot excute statement: " + query);
            }

        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }

        return null;
    }

    public static Connection GetConnection()
    {
        return m_conn;
    }

    private DAOAccess()
    {
        m_server = m_user = m_pass = m_databaseName = "";
    }

    public static void SetDatabaseName(String databaseName)
    {
        m_databaseName = databaseName;
    }

    public static DAOAccess GetInstance()
    {
        return m_instance;
    }


    @Override
    public void finalize() throws Throwable
    {
        if (m_conn != null) m_conn.close();

        super.finalize();
    }
    public static void SetServer(String name)
    {
        m_server = name;
    }

    public static void SetUser(String user)
    {
        m_user = user;
    }

    public static void SetPass(String pass)
    {
        m_pass = pass;
    }

    private DAOAccess(String server, String databaseName, String user, String pass, int port)
    {
        m_server = server;
        m_user = user;
        m_pass = pass;
        m_port = port;
        m_databaseName = databaseName;

        CreateConnection();
    }

    public static void CreateConnection()
    {
        try
        {
            if (m_conn != null)
            {
                m_conn.close();
            }
            com.mysql.jdbc.jdbc2.optional.MysqlDataSource ds  = new com.mysql.jdbc.jdbc2.optional.MysqlDataSource();
            ds.setServerName(m_server);
            ds.setPortNumber(m_port);
            ds.setDatabaseName(m_databaseName);
            ds.setUser(m_user);
            ds.setPassword(m_pass);
            ds.setEncoding("utf-8");

            m_conn = ds.getConnection();
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }
    }

    public static int ExcuteUpdate(String query)
    {
        try
        {
            java.sql.PreparedStatement statement = m_conn.prepareStatement(query);
            if (statement.execute(query))
            {
                return statement.executeUpdate(query, statement.RETURN_GENERATED_KEYS);
            }
            else
            {
                System.out.println("Cannot excute statement: " + query);
            }

        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }

        return -1;
    }

    public static void main(String[] args)
    {
        try
        {

        } catch (Exception e) {
          e.printStackTrace();
        }
    }
}
